import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import Far from './Far'

export default class App extends Component {
  componentDidMount() {
    // 组件挂载成功的时候订阅
    // appCarId 是这次订阅的唯一标识符
    this.appCarId = PubSub.subscribe('car', (topic, data) => {
      console.log('app接收', topic, data)
    })
    this.apprichId = PubSub.subscribe('richFemale', (topic, data) => {
      console.log('app接收', topic, data)
    })
  }
  render() {
    return (
      <div>
        app
        <button
          onClick={() => {
            PubSub.unsubscribe(this.appCarId)
          }}
        >
          清除app组件中car话题的指定订阅
        </button>
        <button
          onClick={() => {
            PubSub.unsubscribe('car')
          }}
        >
          清除car的所有订阅
        </button>
        <button
          onClick={() => {
            PubSub.clearAllSubscriptions()
          }}
        >
          清除所有订阅
        </button>
        <hr />
        <Far></Far>
      </div>
    )
  }
}
